<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="index.css" />

    <style>
      .red {
        color: red;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <div class="container">
        <div class="form-group">
          <div class="input-group">
            <h4>品牌管理</h4>
          </div>
        </div>
        <table class="table table-bordered table-hover mt-2">
          <thead>
            <tr>
              <th>序号</th>
              <th>资产名称</th>
              <th>价格</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item,index) in list">
              <td>{{ index + 1 }}</td>
              <td>{{ item.name }}</td>
              <td :class="{red: item.price >= 100}">{{ item.price }}</td>
              <td>{{ item.time | formatTime}}</td>
              <td>
                <a @click.prevent="list.splice(index, 1)" href="#">删除</a>
              </td>
            </tr>
          </tbody>
        </table>
        <form class="form-inline">
          <div class="form-group">
            <div class="input-group">
              <input
                v-model="name"
                type="text"
                placeholder="资产名称"
                class="form-control"
              />
            </div>
          </div>
          <p></p>
          <div class="form-group">
            <div class="input-group">
              <input
                v-model="price"
                type="text"
                placeholder="价格"
                class="form-control"
              />
            </div>
          </div>
          <p></p>
          <!-- 双标签的button放在form里，那就是submit -->
          <!-- submit有默认的行为：就是提交表单，跳转到表单对应的action，没有action相当于就地刷新 -->
          <!-- 现在需要阻止默认行为 -->
          <button @click.prevent="add" class="btn btn-primary">添加资产</button>
        </form>
      </div>
    </div>

    <script src="./vue.js"></script>
    <script src="./dayjs.min.js"></script>
    <script>
      // 把大盒子交给vue管理
      new Vue({
        el: "#app",
        data: {
          list: [
            { name: "外套", price: 9.9, time: new Date() },
            { name: "内衣", price: 8.9, time: new Date() },
          ],
          name: "",
          price: "",
        },
        methods: {
          add() {
            // // 阻止事件默认行为
            // e.preventDefault()

            // 非空判断
            if (this.name == "" || this.price == "")
              return alert("请输入完整内容");

            // 能来到这证明输入的有内容了
            // 就加到数组里
            this.list.push({
              name: this.name,
              price: this.price,
              time: new Date(),
            });

            // 清空输入的内容
            this.name = this.price = "";
          },
        },
        filters: {
          formatTime(val) {
            return dayjs(val).format("YYYY-MM-DD");
          },
        },
      });
    </script>
  </body>
</html>
